package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.DriverManager;

import com.mbc.common.exception.ConnectionDriverException;

public class DBManager {
	
	public static String Driver = "net.sourceforge.jtds.jdbc.Driver";
	public static String Url = "jdbc:jtds:sqlserver://WIN7ENTERPRICE:1433/ms_mbc;";
	public static String UserName = "sa";
	public static String Password = "123";
	
	public static final int LINKED_IAIMS = 2;
	public static final int LINKED_VB6 = 1;
	
	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String driver;
	private String url;
	private String userName;
	private String password;
	
	
	
	private Connection conn;
	
	public DBManager(String driver, String url, String userName, String password) throws Exception
	{
		this.driver = driver;
		this.url = url;
		this.userName = userName;
		this.password = password;
		
		try {
			Class.forName(this.driver);
			conn = DriverManager.getConnection(this.url, this.userName, this.password);
		} catch (Exception e) {
			throw new ConnectionDriverException(e.getMessage());
		}
	}
	
	public DBManager(Connection conn)
	{
		this.conn = conn;
	}
	
	public Connection getConnection()
	{
		return conn;
	}
	
	public void closeConnection()
	{
		try {
			if(conn != null)
				if(!conn.isClosed())
					conn.close();
		} catch (Exception e) {
			
		}
	}
	
	public void beginTransaction()
	{
		try {
			if(conn != null)
				if(!conn.isClosed())
					conn.setAutoCommit(false);	
		} catch (Exception e) {
			
		}
	}
	
	public void rollBackTransaction()
	{
		try {
			if(conn != null)
				if(!conn.isClosed())
					conn.rollback();
		} catch (Exception e) {
			
		}
	}
	
	public void commitTransaction()
	{
		try {
			if(conn != null)
				if(!conn.isClosed())
					conn.commit();
		} catch (Exception e) {
			
		}
	}
}
